﻿using QRCoder;
using System;
using System.Drawing.Imaging;
using System.IO;
using Volo.Abp.Application.Services;

namespace Colorful.Furniture.QrCode;

/// <summary>
///  二维码
/// </summary>
public class QrCodeAppService : ApplicationService
{
    /// <summary>
    ///  二维码
    /// </summary>
    /// <param name="content">二维码内容</param>
    /// <param name="pixel">像素</param>
    /// <returns></returns>
    public string Get(string content, int pixel = 20)
    {
        var qrGenerator = new QRCodeGenerator();
        var qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);
        var qrCode = new QRCode(qrCodeData);
        var qrCodeImage = qrCode.GetGraphic(pixel);

        using var stream = new MemoryStream();

        if (qrCodeImage != null) qrCodeImage.Save(stream, ImageFormat.Jpeg);

        var bytes = stream.ToArray();

        var base64String = Convert.ToBase64String(bytes);

        return base64String;
    }
}